import { Decimal } from 'decimal.js'
export const publicMixin = {
  data() {
    return {
    }
  },
  methods: {
    //寻找父级节点
    findParentById(tree, parentId) {
      for (let i = 0; i < tree.length; i++) {
        if (tree[i].id === parentId) {
          return tree[i]
        }
        if (tree[i].children) {
          const result = this.findParentById(tree[i].children, parentId)
          if (result) {
            return result

          }
        }
      }
      return null // 如果没有找到父级，返回null
    },
    //是否包含小数
    sDecimal(num) {
      return num.toString().includes('.');
    },
    setParentNode(pNode, name) {
      let total = 0
      pNode.children.forEach((item) => {
        total += item.children.length>0 ? item[name] :  0
      })
      return this.sDecimal(total)?total.toFixed(2)*1:total;
    },
    changeTreeData(nodes, row, name) {
      const pNode = this.findParentById(nodes, row.parentId)
      const isTemporary = row.code?.endsWith('T')
      let num = 0
      pNode.children.forEach((i) => {
        if (isTemporary) {
          if (i.code?.endsWith('T')) {
            num += i[name]
          }
        } else {
          num += i[name]
        }
      })
      pNode[name] = this.sDecimal(num)?num.toFixed(2)*1:num*1;
      if (row.level === 3) {
        //第三层时 计算第一层数据
        const gNode = this.findParentById(nodes, pNode.parentId)
        gNode[name] = this.setParentNode(gNode, name)
      }
      if (row.level === 4) {
        //第四层时 计算第一层数据
        const gNode = this.findParentById(nodes, pNode.parentId)
        const zNode = this.findParentById(nodes, gNode.parentId)
        gNode[name] = this.setParentNode(gNode, name)
        zNode[name] = this.setParentNode(zNode, name)
        this.setFllData(gNode,name)
      }
      return nodes
    },
    setFllData(nodes,name){
      let total = 0
     let newArr = nodes.children.filter(item =>  item.children.length >0)
     let newArrData = nodes.children.filter(item =>  item.children.length == 0)
      newArrData.forEach((item)=>{
        newArr.forEach((i)=>{
          i.children.forEach(j=>{
            if(item.name == j.name){
              let sum = j[name] ? j[name] : 0
              total += sum * 1
            }
          })
        })
        item[name] = this.sDecimal(total)?total.toFixed(2)*1:total;
        total = 0
      })
      return 
    },
    //设置父级节点的值--针对第二种情况
    setParentNodeTwo(pNode, name) {
      let total = 0
      pNode.children.forEach((item) => {
        if (item[name] || (item?.detailValues && item?.detailValues[name])) {
          let sum = item[name] ? item[name] : 0
          total += sum * 1
        }
      })
      return  this.sDecimal(total)?total.toFixed(2)*1:total;
    },
    //-针对第二种情况
    changeTreeDataTwo(nodes, row, name) {
      const pNode = this.findParentById(nodes, row.parentId)
      const isTemporary = row.code?.endsWith('T')
      let num = 0
      pNode.children.forEach((i) => {
        if (i.id === row.id) {  //解决u-table双向绑定不生效
          i.detailValues = row.detailValues

        }
        if (isTemporary) {
          if (i.code?.endsWith('T')) {
            if (i?.detailValues && i?.detailValues[name]) {
              num += i?.detailValues[name] * 1
            }
          }
        } else {
          if (i?.detailValues && i?.detailValues[name]) {
            num += i?.detailValues[name] * 1
          }
        }
      })
      if (pNode.detailValues) {
        pNode.detailValues[name] = this.sDecimal(num)?num.toFixed(2)*1:num*1;
      } else {
        pNode[name] = this.sDecimal(num)?num.toFixed(2)*1:num*1;
      }
      if (row.level === 3) {
        //第三层时 计算第一层数据
        const gNode = this.findParentById(nodes, pNode.parentId)
        gNode[name] = this.setParentNodeTwo(gNode, name)
      }
      return nodes
    },

    //  求和
    summation(data,node){
      data.forEach((item)=>{
        if(item.level == 1){
          node.forEach((i)=>{
            item[i.dataIndex] = this.subsetSum(item.children,i.dataIndex)
          })
        }
      })
    },
    subsetSum(data,name){
      let num  = 0
      data.forEach((item)=>{
        if (item?.detailValues && item?.detailValues[name]) {
          num = new Decimal(num).add(new Decimal(item?.detailValues[name]*1)).toNumber()
        } else{
          num += 0
        }
        if(item.children &&  item.children.length>0){
          this.subsetSum(item.children,name)
        }
      })
      
      return  num.toString();
    }
  }
}